import React, {useEffect, useState} from 'react';
import {getSearchData} from "@/api/posts";
import Link from 'next/link';

interface IProps {
  data: [ISearch];
}

/*
* 组件 : 在components 类型 React.FC
* 页面 :
* */

/**
 * getServerSideProps : 动态路由的时候 利用这个方法获取参数 将返回值映射到 props里面
 * @param context
 */
export const getServerSideProps = async (context: any) => {
  console.log(1)
  let res = await getSearchData();
  if (res) {
    return {
      props: {
        data: res
      }
    }
  }
  return {
    props: {}
  };
}

const PostList: React.FC<IProps> = (props) => {
  const {data} = props;
  const [lists, setLists] = useState<[ISearch]>();

  useEffect(() => {
    // [] : 依赖的项
    // componentDidMount
    //
    // const getAllData = async () => {
    //   // let res = await postProviderList({});
    //   // console.log(res.id);
    // };
    // getAllData()

    setLists(props.data)

  }, []);

  return (
    <ul>
      {data && data.map((item) => (<li
        key={item.uid}>
        <Link href={`/posts/postList/${item.uid}`}>{item.title}</Link>
      </li>))}
    </ul>
  );

}

export default PostList;
